package com.hns.frame.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.hns.frame.security.AppFilterInvocationSecurityMetadataSource;

/**
 * spring容器工具
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class SpringContextUtil implements ApplicationContextAware{
	
	private static ApplicationContext context = null;
	
	@Override
	public void setApplicationContext(ApplicationContext ctx)
			throws BeansException {
		context = ctx;
	}

	public static Object getBean(String beanName){
	    if(context == null){
	        return null;
	    }else{
	        return context.getBean(beanName);
	    }
    }
	
	/**
	 * 根据类型获取spring容器里的bean实例
	 * @param clazz  类名
	 * @return 实例
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	public static <T> T getBean(Class<T> clazz){
        return context.getAutowireCapableBeanFactory().getBean(clazz);
    }
	
	/**
	 * 更新资源缓存
	 * @author:Fisher
	 * @email:zhuangcaijin@126.com
	 */
	public static void updateResMap(){
	    AppFilterInvocationSecurityMetadataSource cfis = (AppFilterInvocationSecurityMetadataSource)SpringContextUtil.getBean("customFilterInvocationSecurityMetadataSource");
	    cfis.refresh();
	}
}
